function NewMog = mog_M_step(X, Q, mog)
    % The M-step of the EM-algorithm

    for i=1:size(Q, 2)
        N(i) = sum(Q{i});
    end
    for i=1:size(mog, 2)
        NewMog{i}.MU = 1/N(i) * sum(([Q{i} Q{i}] .* X(:, 1:2)), 1);
        if(cond(mog{i}.SIGMA) < 10^10)
            SigmaSum = zeros(2, 2);
            for j=1:size(X, 1)
                Temp = (([X(j, 1) - NewMog{i}.MU(1) X(j, 2) - NewMog{i}.MU(2)])' * ([X(j, 1) - NewMog{i}.MU(1) X(j, 2) - NewMog{i}.MU(2)]));
                SigmaSum = SigmaSum + Q{i}(j) * Temp;
            end
            NewMog{i}.SIGMA = 1/N(i) * SigmaSum;
        else
            NewMog{i}.SIGMA = mog{i}.SIGMA
        end
        NewMog{i}.PI = N(i) / size(X(:, 1:2), 1);
    end
